import api from './request'

// 错题本相关接口
export interface WrongQuestion {
  id: number
  userId: number
  questionId: number
  userAnswer: string
  correctAnswer: string
  errorReason: string
  reviewNotes: string
  isReviewed: boolean
  masteryLevel?: number
  practiceSessionId: string
  errorTime: string
  createdAt: string
  updatedAt: string
  
  // 题目信息
  questionContent: string
  questionType: string
  questionDifficulty: string
  questionExplanation: string
  questionImageUrl?: string
  questionCategoryId: number
  questionCategoryName: string
}

export interface WrongQuestionStats {
  totalCount: number
  reviewedCount: number
  unreviewedCount: number
  masteryRate: number
  latestErrorDate: string
}

// 获取错题列表
export const getWrongQuestions = async (params: {
  userId: number
  page?: number
  size?: number
  sortBy?: string
  sortOrder?: string
}) => {
  const response = await api.get('/wrong-questions', { params })
  return response.data
}

// 搜索错题
export const searchWrongQuestions = async (params: {
  userId: number
  keyword: string
  page?: number
  size?: number
}) => {
  const response = await api.get('/wrong-questions/search', { params })
  return response.data
}

// 根据题型获取错题
export const getWrongQuestionsByType = async (params: {
  userId: number
  type: string
  page?: number
  size?: number
}) => {
  const response = await api.get('/wrong-questions/by-type', { params })
  return response.data
}

// 根据复习状态获取错题
export const getWrongQuestionsByReviewStatus = async (params: {
  userId: number
  isReviewed: boolean
  page?: number
  size?: number
}) => {
  const response = await api.get('/wrong-questions/by-review-status', { params })
  return response.data
}

// 更新复习状态
export const updateReviewStatus = async (id: number, isReviewed: boolean, masteryLevel?: number) => {
  const response = await api.put(`/wrong-questions/${id}/review-status`, null, {
    params: { isReviewed, masteryLevel }
  })
  return response.data
}

// 更新错误原因
export const updateErrorReason = async (id: number, errorReason: string) => {
  const response = await api.put(`/wrong-questions/${id}/error-reason`, null, {
    params: { errorReason }
  })
  return response.data
}

// 更新复习笔记
export const updateReviewNotes = async (id: number, reviewNotes: string) => {
  const response = await api.put(`/wrong-questions/${id}/review-notes`, null, {
    params: { reviewNotes }
  })
  return response.data
}

// 删除错题
export const deleteWrongQuestion = async (id: number) => {
  const response = await api.delete(`/wrong-questions/${id}`)
  return response.data
}

// 批量删除错题
export const deleteWrongQuestions = async (ids: number[]) => {
  const response = await api.delete('/wrong-questions/batch', { data: ids })
  return response.data
}

// 清空错题本
export const clearWrongQuestions = async (userId: number) => {
  const response = await api.delete('/wrong-questions/clear', {
    params: { userId }
  })
  return response.data
}

// 获取错题统计信息
export const getWrongQuestionStats = async (userId: number) => {
  const response = await api.get('/wrong-questions/stats', {
    params: { userId }
  })
  return response.data
}
